<template>
  <div class="mod-user">
    <!-- <?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> -->
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.userID"
          placeholder="单据编号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="dataForm.userName"
          placeholder="商品信息"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <div class="block">
          <el-date-picker
            v-model="value1"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="getTime"
          >
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="resetDataList()">重置</el-button>
        <el-button
          v-if="isAuth('sys:user:save')"
          type="primary"
          @click="addOrUpdateHandle()"
          >新增</el-button
        >
        <el-button
          v-if="isAuth('sys:user:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <!-- <el-table-column
        prop="userId"
        header-align="center"
        align="center"
        width="80"
        label="会员卡号">
      </el-table-column> -->
      <el-table-column
        prop="organName"
        header-align="center"
        align="center"
        width="80"
        label="会员卡号"
      >
      </el-table-column>
      <el-table-column
        prop="number"
        header-align="center"
        align="center"
        label="单据编号"
      >
      </el-table-column>
      <el-table-column
        prop="materialsList"
        header-align="center"
        align="center"
        label="商品信息"
      >
      </el-table-column>
      <el-table-column
        prop="operTimeStr"
        header-align="center"
        align="center"
        width="180"
        label="单据日期"
      >
      </el-table-column>
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="操作员"
      >
      </el-table-column>
      <el-table-column
        prop="totalPrice"
        header-align="center"
        align="center"
        label="金额合计"
      >
      </el-table-column>
      <el-table-column
        prop="changeAmount"
        header-align="center"
        align="center"
        label="收款"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('sys:user:update')"
            type="text"
            size="small"
            @click="
              addOrUpdateHandle(
                scope.row.id,
                scope.row.organName,
                scope.row.number,
                scope.row.operTimeStr
              )
            "
            >修改</el-button
          >
          <el-button
            v-if="isAuth('sys:user:delete')"
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./backgoods-add-or-update copy";
export default {
  data() {
    return {
      dataForm: {
        userName: "",
        userID: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      value1: [],
      beginTime: "",
      endTime: "",
    };
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getDataList();
  },
  methods: {
    getTime() {
      this.beginTime = this.timestampToTime(this.value1[0].getTime());
      this.endTime = this.timestampToTime(this.value1[1].getTime());
      console.log(this.beginTime, this.endTime);
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let type = "出库";
      let subType = "零售";
      let roleType = "全部数据";
      let str = `{"type":"${type}","subType":"${subType}","roleType":"${roleType}","state":"","number":"${this.dataForm.userID}","beginTime":"${this.beginTime}","endTime":"${this.endTime}","materialParam":"${this.dataForm.userName}","depotIds":""}`;
      var search = encodeURIComponent(str);
      // console.log(search)
      this.$http({
        url: this.$http.adornUrl(`/depotHead/list?search=${search}`),
        method: "get",
        params: this.$http.adornParams({
          currentPage: this.pageIndex,
          pageSize: this.pageSize,
        }),
      }).then(({ data }) => {
        // console.log(data);
        if (data && data.code === 0) {
          this.dataList = data.data.list;
          this.totalPage = data.data.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 时间戳转成时间
    timestampToTime(timestamp) {
      var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + "-";
      var M =
        (date.getHours() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var D = date.getDate() + " ";
      var h = date.getHours() + ":";
      var h = date.getHours() + ":";
      var m = date.getMinutes() + ":";
      var s = date.getSeconds();
      return Y + M + D + h + m + s;
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id, organName, number, operTimeStr) {
      // console.log(id)
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, organName, number, operTimeStr);
      });
    },
    // 删除
    deleteHandle(id) {
      var userIds = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.userId;
          });
      this.$confirm(
        `确定对[id=${userIds.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          // this.$http({
          //   url: this.$http.adornUrl(
          //     "/depotHead/batchDeleteDepotHeadAndDetail"
          //   ),
          //   method: "post",
          //   // data: this.$http.adornData(userIds, false),
          //   data: this.$http.adornData({
          //     ids: 246,
          //   }),
          // }).then(({ data }) => {
          //   console.log(data);
          //   if (data && data.code === 0) {
          //     this.$message({
          //       message: "操作成功",
          //       type: "success",
          //       duration: 1500,
          //       onClose: () => {
          //         this.getDataList();
          //       },
          //     });
          //   } else {
          //     this.$message.error(data.msg);
          //   }
          // });

           this.$http({
            url: this.$http.adornUrl(
              "/depotHead/deleteDepotHeadAndDetail?id=276"
            ),
            method: "post",
            // data: this.$http.adornData(userIds, false),
            // data: this.$http.adornData({
            //   ids: 246,
            // }),
          }).then(({ data }) => {
            console.log(data);
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },

    // 重置
    resetDataList() {
      // console.log(11)
      this.dataForm.userName = "";
      this.dataForm.userID = "";
      this.value1 = "";
      this.beginTime = "";
      this.endTime = "";
      console.log(this.beginTime, this.endTime);
      this.getDataList();
    },
  },
};
</script>
